﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DTO;
using DataService;

public partial class UserControls_LocationControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ( !IsPostBack )
        {
            using ( ServiceHelper service = new ServiceHelper() )
            {
                ddlState.DataSource = service.GetStates();
                ddlCountry.DataSource = service.GetCountries();
            }

            DataBind();
        }
    }

    public LocationInfo Location
    {
        get
        {
            return new LocationInfo(0)
            {
                Address = txtStreet1.Text,
                Street = txtStreet2.Text,
                City = txtCity.Text,
                State = new State() { Code = ddlState.SelectedValue, Name = ddlState.SelectedItem.Text },
                Country = new Country() { Code = ddlCountry.SelectedValue, Name = ddlCountry.SelectedItem.Text }
            };
        }
        set
        {
            txtStreet1.Text = value.Address;
            txtStreet2.Text = value.Street;
            txtCity.Text = value.City;

            if ( !string.IsNullOrEmpty(value.State.Name) )
                ddlState.SelectedValue = value.State.Code;

            if ( !string.IsNullOrEmpty(value.Country.Name) )
                ddlCountry.SelectedValue = value.Country.Code;
        }
    }
}